package com.example.takeaway.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.example.takeaway.entity.Category;
import com.example.takeaway.serivce.ICategoryService;
import com.example.takeaway.utils.PageResult;
import com.example.takeaway.utils.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * categoryController
 * 
 * @author ruoyi
 * @date 2025-05-15
 */
@RestController
@RequestMapping("/system/category")
public class CategoryController
{
    @Autowired
    private ICategoryService categoryService;

    /**
     * 查询category列表
     */
//    @PreAuthorize("@ss.hasPermi('system:category:list')")
    @GetMapping("/list")
    public ServerResponse list(Category category, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10")int pageSize)
    {
//        startPage();
        PageResult pageResult = categoryService.selectCategoryList(category,pageNum,pageSize);
        return  ServerResponse.createBysuccess(200,pageResult,"查询成功");
    }

    /**
     * 导出category列表
     */
//    @PreAuthorize("@ss.hasPermi('system:category:export')")
//    @Log(title = "category", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Category category)
//    {
//        List<Category> list = categoryService.selectCategoryList(category);
//        ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
//        util.exportExcel(response, list, "category数据");
//    }

    /**
     * 获取category详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:category:query')")
    @GetMapping(value = "/{catid}")
    public ServerResponse getInfo(@PathVariable("catid") Long catid)
    {

        Category category = categoryService.selectCategoryByCatid(catid);
        return ServerResponse.createBysuccess(200,category,"查询成功");
    }

    /**
     * 新增category
     */
//    @PreAuthorize("@ss.hasPermi('system:category:add')")
//    @Log(title = "category", businessType = BusinessType.INSERT)
    @PostMapping
    public ServerResponse add(@RequestBody Category category)
    {
        category.setCreateTime(new Date());
        category.setCreateBy("admin");
        int i  = categoryService.insertCategory(category);
        if (i>0){
            return  ServerResponse.createBysuccess(200,"","新增成功");
        }else{
            return  ServerResponse.createByfail(400,"新增失败");
        }
    }

    /**
     * 修改category
     */
//    @PreAuthorize("@ss.hasPermi('system:category:edit')")
//    @Log(title = "category", businessType = BusinessType.UPDATE)
    @PutMapping
    public ServerResponse edit(@RequestBody Category category)
    {
        category.setUpdateTime(new Date());
        category.setUpdateBy("admin");
        int i = categoryService.updateCategory(category);
        if (i>0){
            return  ServerResponse.createBysuccess(200,"","新增成功");
        }else{
            return  ServerResponse.createByfail(400,"新增失败");
        }
    }

    /**
     * 删除category
     */
//    @PreAuthorize("@ss.hasPermi('system:category:remove')")
//    @Log(title = "category", businessType = BusinessType.DELETE)
	@DeleteMapping("/{catids}")
    public ServerResponse remove(@PathVariable Long[] catids)
    {
        int i = categoryService.deleteCategoryByCatids(catids);
        if (i>0){
            return  ServerResponse.createBysuccess(200,"","新增成功");
        }else{
            return  ServerResponse.createByfail(400,"新增失败");
        }
    }
}
